package com.tanhua.db.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import com.tanhua.api.UserInfoApi;
import com.tanhua.db.mapper.UserInfoMapper;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 新增用户信息
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 修改用户信息
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    /**
     * 分页查询推荐好友列表的用户详情
     * @param ids
     * @param recommendUserDto
     * @return
     */
    @Override
    public Page<UserInfo> findByIds(List<Long> ids, RecommendUserDto recommendUserDto) {
        //1.创建分条件查询对象
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();

        //2，设置查询条件
        //根据好友用户id查询用户的详情
        queryWrapper.in(UserInfo::getId, ids);
        //判断recommendUserDto（前端页面传递的参数也存在部分查询条件：年龄，学历等）中是否还存在需要判断的条件
        if (recommendUserDto != null){
            //性别条件查询
            queryWrapper.eq(StringUtils.isNoneBlank(recommendUserDto.getGender()), UserInfo::getGender,recommendUserDto.getGender());
            //年龄条件查询(由于前端页面年龄参数为30.导致结果不值观)
//            if (recommendUserDto.getAge() !=null){
//                recommendUserDto.setAge(25);
//                queryWrapper.le(UserInfo::getAge,recommendUserDto.getAge());
            queryWrapper.le(recommendUserDto.getAge() !=null,UserInfo::getAge,recommendUserDto.getAge());
        }

        //3.创建分页对象进行查询
        Page<UserInfo> pages = new Page<>(recommendUserDto.getPage(), recommendUserDto.getPagesize());

        //4.执行查询
        Page<UserInfo> page = userInfoMapper.selectPage(pages, queryWrapper);

        //5.返回结果
        return page;
    }


    /**
     * 分页查询好友动态
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> findFriendIds(List<Long> ids) {
        //1.创建查询对象
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();

        //2.添加查询条件
        queryWrapper.in(UserInfo::getId,ids);

        //3.执行查询
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);

        return userInfoList;
    }


    /**
     * 查询评论列表信息
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> findByUserId(List<Long> ids) {
        //1.创建查询对象
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();

        //2.添加查询条件
        queryWrapper.in(UserInfo::getId,ids);

        //3.执行查询
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }


    /**
     * 根据好友id查询好友的详情（分页查询）
     * @param page
     * @param pagesize
     * @param friendIds
     * @param keyword
     * @return
     */
    @Override
    public List<UserInfo> findByFriends(Integer page, Integer pagesize, List<Long> friendIds, String keyword) {
        //1.构建查询条件对象
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        //2.添加查询条件
        queryWrapper.in(CollUtil.isNotEmpty(friendIds),UserInfo::getId,friendIds);
        queryWrapper.like(StringUtils.isNoneBlank(keyword),UserInfo::getNickname,"%"+keyword+"%");
        //3.分页查询
        Page<UserInfo> pages = new Page<>(page,pagesize);
        pages = userInfoMapper.selectPage(pages,queryWrapper);
        return pages.getRecords();
    }

    /**
     * 探花搜索附近用户时，通过附近用户ids带条件查询（gender）
     * @param userIds
     * @param gender
     * @return
     */
    @Override
    public List<UserInfo> findByUserIds(List<Long> userIds, String gender) {
        //1.构建查询条件
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CollUtil.isNotEmpty(userIds), UserInfo::getId, userIds);
        queryWrapper.eq(StringUtils.isNoneBlank(gender), UserInfo::getGender, gender);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }
}
